package com.example.administrator.myapplication.login;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.HomeActivity;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.register.RegisterActivity;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import rx.subscriptions.CompositeSubscription;





public class LoginFragment extends Fragment{

    private TextView login_to_register_tv;
    private EditText login_user_et;
    private EditText login_pwd_et;
    private Button   login_bt;
    private ImageView login_iv_cancel;
    private SharedPreferences sp;
    private TextView login_tv_register;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
          View view= inflater.inflate(R.layout.fragment_login,container,false);
          //sharepreference 使用键值对来保存
          sp=getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
          login_to_register_tv=(TextView) view.findViewById(R.id.login_register_tv);
          login_user_et=(EditText) view.findViewById(R.id.login_user_et);
          login_pwd_et=(EditText) view.findViewById(R.id.login_pwd_et);
          login_bt=(Button)view.findViewById(R.id.login_bt);
          login_iv_cancel= (ImageView) view.findViewById(R.id.iv_login_cancel);
          login_tv_register= (TextView) view.findViewById(R.id.tv_login_register);

        login_iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(),HomeActivity.class);
                startActivity(intent);
            }
        });

        login_tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(),RegisterActivity.class);
                startActivity(intent);
            }
        });

        login_bt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  testLogin();
              }
          });

          login_to_register_tv.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent=new Intent(getActivity(), RegisterActivity.class);
                  startActivity(intent);
              }
          });


          return view;
    }




    /**
     * 注意下如果返回206错误 一般是多设备登录导致
     */
    private void testLogin() {
        final BmobUser bmobUser=new BmobUser();
        bmobUser.setUsername(login_user_et.getText().toString());
        bmobUser.setPassword(login_pwd_et.getText().toString());
        //login回调
        bmobUser.login(new SaveListener<BmobUser>() {

			@Override
			public void done(BmobUser bmobUser, BmobException e) {
				if(e==null){
                    Toast.makeText(getActivity(),"登录成功",Toast.LENGTH_SHORT).show();
                    //用shareperference来记录登录状态
                    SharedPreferences.Editor editor =sp.edit();
                    editor.putBoolean("login",true);
                    editor.commit();
                    //运用一个文档记录objectid
                    File file =new File(getActivity().getFilesDir(),"objectid.txt");
                    try {
                        BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(file));
                        bos.write(bmobUser.getObjectId().getBytes());
                        bos.close();

                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    Intent intent = new Intent(getActivity(), HomeActivity.class);
                    startActivity(intent);

				}else{
					Toast.makeText(getActivity(),"用户名不存在或者密码错误",Toast.LENGTH_SHORT).show();
				}
			}
		});

    }



}
